package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.DailySummaryEntity;

/**
 * author:        yupeng
 * created on:    2019/7/11 14:42
 * description:
 */
public class ADA_DailySummary extends RecyclerView.Adapter<ADA_DailySummary.ViewHolder> {
    public static final int TYPE_TITLE = 1;
    public static final int TYPE_BODY = 2;

    private List<DailySummaryEntity> mData;

    private Context mContext;

    public ADA_DailySummary(Context context) {
        this.mContext = context;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == TYPE_TITLE) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_daily_title, null);
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_daily_body, null);
        }
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                DisplayUtil.dip2px(mContext, 50));
        view.setLayoutParams(params);
        return new ViewHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        int viewType = getItemViewType(position);

        DailySummaryEntity entity = mData.get(position);

        final int tip = entity.getTip();

        if (viewType == TYPE_TITLE) {
            holder.tvDailyTitle.setText(entity.getTitle());
            holder.tvDailyPrice.setText(String.format("￥%s", StringUtils.getFormattedMoney(entity.getPrice())));
            holder.viewTitleLine.setVisibility(position != getItemCount() - 1 ? View.VISIBLE : View.GONE);
            holder.ivTitleTip.setVisibility(tip != -1 ? View.VISIBLE : View.GONE);
            holder.ivTitleTip.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mListener!=null){
                        mListener.onItemSelected(v,mContext.getString(tip));
                    }
                }
            });

        } else if (viewType == TYPE_BODY) {
            holder.tvBodyTitle.setText(String.format("- %s", entity.getTitle()));
            holder.tvBodyPrice.setText(String.format("￥%s", StringUtils.getFormattedMoney(entity.getPrice())));
            holder.viewBodyLine.setVisibility(position != getItemCount() - 1 ? View.VISIBLE : View.GONE);
            holder.ivBodyTip.setVisibility(tip != -1 ? View.VISIBLE : View.GONE);
            holder.ivBodyTip.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mListener!=null){
                        mListener.onItemSelected(v,mContext.getString(tip));
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return ListUtil.isListEmpty(mData) ? 0 : mData.size();
    }

    public void setData(List<DailySummaryEntity> mData) {
        this.mData = mData;
    }

    @Override
    public int getItemViewType(int position) {
        DailySummaryEntity entity = mData.get(position);
        return entity.getType();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvDailyTitle;
        TextView tvDailyPrice;
        View viewTitleLine;
        ImageView ivTitleTip;

        TextView tvBodyTitle;
        TextView tvBodyPrice;
        View viewBodyLine;
        ImageView ivBodyTip;

        public ViewHolder(View itemView, int viewType) {
            super(itemView);
            if (viewType == TYPE_TITLE) {
                tvDailyTitle = itemView.findViewById(R.id.tv_daily_title);
                tvDailyPrice = itemView.findViewById(R.id.tv_daily_price);
                viewTitleLine = itemView.findViewById(R.id.view_title_line);
                ivTitleTip = itemView.findViewById(R.id.iv_title_tip);
            } else {
                tvBodyTitle = itemView.findViewById(R.id.tv_body_title);
                tvBodyPrice = itemView.findViewById(R.id.tv_body_price);
                viewBodyLine = itemView.findViewById(R.id.view_body_line);
                ivBodyTip = itemView.findViewById(R.id.iv_body_tip);
            }
        }
    }

    public interface OnItemTipClickedListener {

        void onItemSelected(View view, String tip);

    }

    private OnItemTipClickedListener mListener;

    public void setOnItemTipClickedListener(OnItemTipClickedListener mListener) {
        this.mListener = mListener;
    }
}
